<template>
  <!-- 考勤统计 - 个人-->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" @click="exportAttendanceList"
          >导出考勤</el-button
        >
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-date-picker
            v-model="searchValue.time"
            type="month"
            placeholder="请选择"
            style="width: 100%"
            value-format="YYYY-MM"
          />
          <!-- <el-input
            v-model="searchValue.search"
            placeholder="日程内容"
            clearable
          ></el-input> -->
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        :params="params"
        row-key="id"
        stripe
        @filterType="filterType"
      >
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column label="姓名" prop="username" width="200">
        </el-table-column>
        <el-table-column
          label="日期"
          prop="createTime"
          width="300"
        ></el-table-column>
        <el-table-column label="上班" prop="inAttendanceTime">
          <template #default="scope">
            <div
              class="timeCell"
              v-for="item in scope.row.attendanceChildList"
              :key="item.id"
            >
              <!-- clockType 1上班 2下班 -->
              <template v-if="item.clockType == 1">
                <p>时间：{{ item.attendanceTime }}</p>
                <!-- <p>地点：{{ item.address }}</p> -->
                <!-- 0未打卡 1正常 2迟到 3早退 4异常（外勤）-->
                <div>
                  <p>状态：</p>
                  <p style="color: #67c23a" v-if="item.inClockType == 1">
                    正常
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 0">
                    缺卡
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 2">
                    迟到
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 4">
                    异常
                  </p>
                </div>
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="下班" prop="outAttendanceTime">
          <template #default="scope">
            <div
              class="timeCell"
              v-for="item in scope.row.attendanceChildList"
              :key="item.id"
            >
              <!-- clockType 1上班 2下班 -->
              <template v-if="item.clockType == 2">
                <p>时间：{{ item.attendanceTime }}</p>
                <!-- <p>地点：{{ item.address }}</p> -->
                <!-- 0未打卡 1正常 2迟到 3早退 4异常-->
                <div>
                  <p>状态：</p>
                  <p style="color: #67c23a" v-if="item.inClockType == 1">
                    正常
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 0">
                    缺卡
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 3">
                    早退
                  </p>
                  <p style="color: #f56c6c" v-if="item.inClockType == 4">
                    异常
                  </p>
                </div>
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="外勤打卡"
          prop="attendanceTypeName"
          min-width="100"
        >
          <template #default="scope">
            <el-button
              text
              type="primary"
              size="small"
              @click="table_show(scope.row, scope.$index)"
              v-if="scope.row.outChildList.length != 0"
              >{{ scope.row.outChildList.length }}次</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="attendanceTypeName" min-width="100">
          <template #default="scope">
            <!-- 1正常 2请假 3异常  -->
            <p style="color: #67c23a" v-if="scope.row.attendanceType == 1">
              正常
            </p>
            <p style="color: #ff9c02" v-if="scope.row.attendanceType == 2">
              请假
            </p>
            <p style="color: #f56c6c" v-if="scope.row.attendanceType == 3">
              异常
            </p>
            <p v-if="scope.row.attendanceLeave.length != 0">
              【{{ scope.row.attendanceLeave[0].leaveTypeName }}】{{
                scope.row.attendanceLeave[0].startTime
              }}--{{ scope.row.attendanceLeave[0].endTime }}
            </p>
          </template>
        </el-table-column>
        <el-table-column
          label="补卡原因"
          prop="attendanceTypeName"
          min-width="100"
        >
        </el-table-column>
      </scTable>
    </el-main>
    <!-- 外勤打卡明细 -->
    <out-list ref="outList"></out-list>
  </el-container>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
import { exportFile } from '@/utils/exportFile'
import outList from './components/outList.vue'
export default {
  name: 'attendance',
  components: {
    outList
  },
  data () {
    return {
      dialog: {
        save: false,
      },
      apiObj: this.$API.attendance.getAttendanceItem,
      params: { adminId: this.$route.query.id },
      searchValue: {
        search: '',
        time: this.$TOOL.dateFormat(new Date(), 'yyyy-MM')
      }
    }
  },
  beforeMount () {
    // let userId = this.$route.query.id
    // if (userId) {
    // this.params.adminId = userId
    // }


  },

  methods: {
    // 查看外勤打卡明细
    table_show (row) {
      this.$refs.outList.open(row)
    },
    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    // 处理类型
    filterType (data) {
      let tableData = data
      tableData.forEach(item => {
        if (item.attendanceLeave.length != 0) {
          queryTypeByDic("leaveType").then((res) => {
            let list = res
            item.attendanceLeave.forEach((item) => {
              list.forEach((t_item) => {
                if (item.leaveType == t_item.value) {
                  item.leaveTypeName = t_item.name
                }
              })
            })
          })
        }
      })
      console.log(tableData)
    },
    // 导出
    async exportAttendanceList () {
      let res = await this.$API.attendance.exportAttendanceItem.get({ adminId: this.$route.query.id, time: this.searchValue.time })
      exportFile(res, '考勤明细.xls')
    }
  }
}
</script>

<style lang='scss' scoped>
.timeCell {
  display: flex;
  flex-direction: column;
  p {
    display: inline-block;
    // margin-right: 10px;
  }
}
</style>
